<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
            v-model="queryParams.itemName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['wm:kuling:add']">库龄分组设置</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--      margin-top: 10px;-->
<!--      <div style="color: #ff4d4f; font-size: 12px;  padding-left: 20px">
        <i class="el-icon-info" style="margin-right: 5px"></i>
        默认分组:1-30天、31-60天、61-90天、91天以上，可自定义
      </div>-->
    </el-card>
    <div style="height: 15px"></div>
<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wm:kuling:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->
    <el-card style="border-radius: 10px">
    <!-- 动态表格 -->
    <el-table :data="reportData"  style="width: 100%">
      <!-- 固定列 -->
      <el-table-column prop="itemCode" label="物料编码" fixed />
      <el-table-column prop="itemName" label="物料名称" align="center"/>
      <el-table-column prop="specification" label="规格型号" align="center"/>
      <el-table-column prop="unitOfMeasure" label="单位" align="center"/>
      <el-table-column prop="stockQuantity" label="库存数量" align="center"/>

      <!-- 动态期间列 -->

      <!-- 动态生成日期列 -->
      <!-- 动态期间列 -->
      <el-table-column
          v-for="(period, index) in periodColumns"
          :key="index"
          :label="period.label"
          align="center">
        <template slot-scope="{ row }">
          {{ getPeriodValue(row, period.period) }}
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <add-rule ref="klrule" v-if="devisible" @refreshDataList="getList"></add-rule>
  </div>
</template>

<script>
import { listKuling, getKuling, delKuling, addKuling, updateKuling,stockAgeAnalysis } from "@/api/mes/wm/kulingrule";
import AddRule from "./addrule.vue"
import ConversionsLine from "@/views/mes/wm/materialconversions/conversionsline.vue";
export default {
  name: "Kuling",
  components:{ConversionsLine, AddRule},
  data() {
    return {
      devisible:false,
      reportData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库龄f分析规则表格数据
      kulingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: null,
      itemName:null},
      // 表单参数
      // form: {},
      form: {
        periods: [{ start: 0, end: null }]
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    periodColumns() {
      if (!this.reportData.length) return [];

      // 获取第一个物件的期间配置作为列定义
      return this.reportData[0].periodDataList
          .sort((a, b) => a.sortOrder - b.sortOrder)
          .map(p => ({
            period: p.period,
            label: p.period.replace("~", "-")
          }));
    }
  },
  methods: {
    getPeriodValue(row, period) {
      const target = row.periodDataList.find(p => p.period === period);
      return target ? target.periodQuantity : '-';
    },
    /** 查询库龄f分析规则列表 */
    getList() {
      this.loading = true;
    /*  listKuling(this.queryParams).then(response => {
        this.kulingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });*/

      stockAgeAnalysis(this.queryParams).then(response => {
        console.log("11111",response.rows)
        this.reportData = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log("222222",this.reportData)
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        groupName: null,        startDays: null,        endDays: null,        intervalDays: null,        isSystemDefault: null,        sortOrder: null,        createBy: null,        createByName: null,        createTime: null,        tenantId: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      /*this.reset();
      this.open = true;
      this.title = "添加库龄f分析规则";*/
      this.devisible=true
      this.$nextTick(() => {
        this.$refs.klrule.init()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKuling(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库龄f分析规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKuling(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKuling(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库龄f分析规则编号为"' + ids + '"的数据项？').then(function() {
        return delKuling(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wm/kuling/export', {
        ...this.queryParams
      }, `kuling_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
